<?php
/*
 * Copyright (c) 2021.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\adminapi\controller\v1\platform;

use app\common\model\addon\Addons;
use app\common\model\platform\Platform;
use app\common\model\platform\PlatformApp as PlatformAppModel;
use app\common\model\platform\PlatformAppUser;
use app\common\model\user\User;
use app\common\model\user\UserPermission;
use app\common\model\wework\WeworkApps;
use FormBuilder\Driver\CustomComponent;
use FormBuilder\Driver\FormComponent;
use hiduoke\services\CloudServer;
use hiduoke\services\FormBuilder as Form;
use think\App;
use think\facade\Cache;

/**
 * 平台应用小程序
 */
class PlatformApp extends PlatformController
{
    /**
     * @var PlatformAppModel
     */
    private PlatformAppModel $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PlatformAppModel();
    }

    /**
     * 获取小程序信息
     * @param $plaid
     * @param $appid
     * @return \think\response\Json
     */
    public function appInfo($plaid = 0, $appid = 0)
    {
        $plaid = $plaid ?: get_plaid();
        $appid = $appid ?: get_appid();
        $res = $this->model->where(['plaid' => $plaid, 'id' => $appid])->find();
        if (!$res) {
            return $this->error('未找到当前小程序');
        }
        $addon = (new Addons())->where(['name' => $res['addon_name']])->find();
        if ($res['logo']) {
            $addon['icon'] = $res['logo'];
        }
        if ($res['name']) {
            $addon['title'] = $res['name'];
        }
        return $this->success('', $addon);
    }

    public function getPlatformAppList()
    {
        $user = get_user();
        $platform = Platform::where(['plaid' => get_plaid()])->find();
        if (!$platform) {
            return $this->error("平台有误");
        }
        $where = [];
        if (!$user['is_admin'] && $platform['uid'] != $user['uid']) {
            // 如果不是管理员，而是子账号
            $addon_names = (new UserPermission())->getAddonNames($user['uid'], $platform['plaid']);
            $where = [['addon_name', 'in', $addon_names]];
        }
        $data = $this->model->dkWhere($where)->getPageListModel()->append(['addon'])->withSearch(['addon'])->select();
        $count = $this->model->dkWhere($where)->getPageListModel()->append(['addon'])->withSearch(['addon'])->count();

        foreach ($data as $key => $value) {
            $data[$key]['isDefault'] = false;
            if ($platform['default_app_id'] == $value['id']) {
                $data[$key]['isDefault'] = true;
            }
        }
        return $this->success($count, $data);
    }


    /**
     * 添加应用小程序
     * @param $aid
     * @param $addon_name
     * @param $plaid
     */
    public function addPlatformAddonApp($aid, $addon_name, $plaid)
    {
        $platform = (new Platform())->where(['plaid' => $plaid])->find();
        if (!$platform) {
            return $this->error('平台不存在');
        }
        $addon = (new Addons())->where(['name' => $addon_name])->find();
        if (!$addon) {
            return $this->error('应用不存在');
        }
        // 已使用次数
        $platform_app_count = $this->model->where(['aid' => $aid, 'addon_name' => $addon_name, 'plaid' => $plaid])->count();
        // 总次数
        $platform_app_user = (new PlatformAppUser())->where(['addon_name' => $addon_name, 'uid' => $platform['uid']])->find();

        if (!$platform_app_user || $platform_app_user['count'] <= $platform_app_count) {
            return $this->error('当前应用可添加次数不足');
        }
        // 获取平台信息
        $cloud_data = (new CloudServer())->getSiteAddonTokenCount(false);
        if (isset($cloud_data[$addon_name]) && $cloud_data[$addon_name]['surplus_count'] <= 0) {
            return $this->error("添加失败，应用token授权次数不足");
        }
        $token_res = (new CloudServer())->getSiteAddonToken($addon_name, $plaid, true);
        // 判断小程序次数
        if ($token_res['code'] == -1) {
            // 应用无法添加
            return $this->error($token_res['msg']);
        } else {
            if ($token_res['code'] == -2) {
                // 本地应用
                $token = 'local';
            } else {
                // 线上应用
                if (isset($token_res['data']['token'])) {
                    $token = $token_res['data']['token'];
                } else {
                    return $this->error("添加失败，token授权失败");
                }
            }
            $app_data = array(
                'uid'        => $platform['uid'],
                'aid'        => $aid,
                'plaid'      => $plaid,
                'addon_name' => $addon_name,
                'app_type'   => $addon['app_type'],
                'token'      => $token
            );
            $this->model->save($app_data);
            (new Platform())->where(['plaid' => $plaid])->update(['default_app_id' => $this->model->id]);
            Cache::delete('getSiteAddonTokenCount2');
            return $this->success('添加成功');
        }
    }

    public function getPlatformAddonAppForm()
    {
        $id = input("id", 0);
        $action = "/adminapi/platform/app/platformAddonAppFormSave";
        $platform = (new Platform())->where(['plaid' => get_plaid()])->find();
        if (!$platform) {
            return $this->error('平台不存在');
        }
        if (!$id) {
            $id = $this->model->where(['plaid' => get_plaid(), 'addon_name' => get_addon()])->value('id');
        }
        $rule = array();
        $form = Form::createForm($action)->setMethod("POST");
        $row = $this->model->where(['id' => $id])->findOrEmpty();
        $col = Form::col()->span(11)->offset(1)->pull(1);
        $rule[] = Form::hidden('id', $row->id);
        $rule[] = Form::switches('index', '首页显示', strval($row->index))->col($col)->effect(['help' => '显示到客户端首页']);
        $rule[] = Form::switches('menu', '菜单显示', strval($row->menu))->col($col)->effect(['help' => '显示到平台左侧列表页']);
        $rule[] = Form::dkImage('logo', '图标', $row['logo'] ?? '')->effect(['help' => '自定义小程序图标,方便区分']);
        $rule[] = Form::input('name', '名称', $row['name'] ?? '')->effect(['help' => '自定义小程序名称,方便区分']);
        if ($this->userInfo['is_admin']) {
            $rule[] = Form::input('token', 'Token', $row['token'] ?? '')->effect(['help' => '小程序应用秘钥，仅管理员可见，对应怪兽商城授权token'])->disabled(true);
        }
        if (input('is_admin', 0) && $this->userInfo['is_admin']) {
            $rule[] = Form::input('token', "应用Token", $row['token'] ?? '')->effect(['help' => '平台下的应用Token，请勿随意修改，可能会导致应用部分云端功能无法使用,仅管理员可见']);
        }
        $rule[] = Form::input('sort', '排序', $row['sort']);
        $addon_rules = event($row['addon_name'] . '_platform_addon_config', $row['config'])[0] ?? [];
        if (!$addon_rules) {
            $instance = get_addons_instance($row['addon_name']);
            if ($instance !== null && method_exists($instance, 'platform_addon_config')) {
                $addon_rules = $instance->platform_addon_config($row['config']);
            }
        }
        if (is_array($addon_rules)) {
            foreach ($addon_rules as $key => $value) {
                if ($value instanceof FormComponent) {
                    $rule[] = $value;
                } elseif ($value instanceof CustomComponent) {
                    $rule[] = (new CustomComponent('elCol'))->appendChild($value)->props(['style' => 'margin-bottom:22px']);
                }
            }
        }
        $form->setTitle("平台应用小程序配置")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function platformAddonAppFormSave($formData = [])
    {
        $id = $formData['id'];
        $res = $this->model->where(['id' => $id])->find();
        if ($res) {
            $res->index = $formData['index'];
            $res->menu = $formData['menu'];
            $res->sort = $formData['sort'];
            $res->logo = $formData['logo'];
            $res->name = $formData['name'];
            if (isset($formData['token'])) {
                $res->token = $formData['token'];
            }
            unset($formData['index']);
            unset($formData['menu']);
            unset($formData['sort']);
            unset($formData['logo']);
            unset($formData['name']);
            $res->config = $formData;
            $res->save();
            return $this->success("修改成功");
        } else {
            return $this->error('修改失败，未找到对应应用');
        }
    }
}
